package site.wetsion.employ.server.service;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import site.wetsion.employ.server.controller.UserController;
import site.wetsion.employ.server.mapper.CompanyMapper;
import site.wetsion.employ.server.mapper.UserMapper;
import site.wetsion.employ.server.pojo.Company;
import site.wetsion.employ.server.pojo.CompanyDetail;
import site.wetsion.employ.server.pojo.CompanyProduct;
import site.wetsion.employ.server.pojo.EnterpriseUser;
import site.wetsion.employ.server.pojo.User;
import site.wetsion.employ.server.util.Page;

@Service
public class EnterpriseUserService {
	
	private final static Logger log = LoggerFactory.getLogger(EnterpriseUserService.class);


	@Autowired
	private UserMapper userMapper;

	@Autowired
	private CompanyMapper companyMapper;
	
	/** 根据用户id获取其企业用户*/
	public EnterpriseUser getEnterpriseUserByUid(BigInteger userId){
		try {
			return userMapper.getEnterpriseUserByUid(userId);
		} catch (Exception e) {
			log.error("根据用户查询企业用户表失败，用户id="+userId, e);
			return null;
		}
	}
	
	/** 提交企业用户认证，即绑定公司，即插入一条企业用户表记录*/
	public boolean bindcompany(EnterpriseUser user){
		Timestamp now = new Timestamp(System.currentTimeMillis());
		user.setGmt_create(now);
		user.setGmt_modified(now);
		user.setStatus(0);
		try {
			userMapper.insertEnterpriseUser(user);
			return true;
		} catch (Exception e) {
			log.error("插入企业用户失败", e);
			return false;
		}
	}
	
	/** 根据id获取其企业用户*/
	public EnterpriseUser getEnterpriseUserByid(BigInteger id){
		try {
			return userMapper.getEnterpriseUserByid(id);
		} catch (Exception e) {
			log.error("根据id查询企业用户表失败，id="+id, e);
			return null;
		}
	}
	
	public boolean modifyEnterpriseUserStatus(BigInteger id,int status){
		try {
			userMapper.updateEnterpriseUserStatus(id, status);
			return true;
		} catch (Exception e) {
			log.error("修改企业用户状态失败", e);
			return false;
		}
	}
	
	public int getUncheckEnterpriseUserCount(){
		try {
			return userMapper.getUncheckEnterpriseUserCount();
		} catch (Exception e) {
			log.error("获取魏审核企业用户数量失败", e);
			return 0;
		}
	}
	
	public List<EnterpriseUser> getUncheckEnterpriseUserList(Page page){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			return userMapper.getUncheckEnterpriseUserList((pageNum-1)*pageSize, pageSize);
		} catch (Exception e) {
			log.error("获取未审核企业用户失败", e);
			return null;
		}
	}
	
	/**
	 * 分页查询未审核企业列表
	 * @return
	 */
	public List<Company> getAllUncheckCompanyByPage(Page page){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			List<Company> companys = companyMapper.getUncheckCompanysByPage((pageNum-1)*pageSize, pageSize);
			for (Company company : companys) {
				String location_name = companyMapper.getLocationNameById(company.getCompany_location());
				System.out.println("a");
				company.setLocation_name(location_name);
				User user = userMapper.findUserById(company.getCompany_creater());
				company.setCreater(user);
			}
			return companys;
		} catch (Exception e) {
			log.error("分页查询公司出错", e);
			return null;
		}
	}
	
	/**
	 * 查询未审核公司数
	 * @return
	 */
	public int getUncheckCompanyCount(){
		try {
			int num = companyMapper.getUncheckCompanyCount();
			return num;
		} catch (Exception e) {
			log.error("查询tb_company_uncheck总记录数失败", e);
			return 0;
		}
	}
	
	/** 删除未审核企业*/
	public boolean deleteEnterpriseUncheck(BigInteger id){
		try {
			companyMapper.deleteUncheckCompanyById(id);
			return true;
		} catch (Exception e) {
			log.error("删除未审核企业失败", e);
			return false;
		}
	}
	
	/** 删除未审核公司详情*/
	public boolean deleteCompanyDetailUncheck(BigInteger id){
		try {
			companyMapper.deleteCompanyDetailUncheckById(id);
			return true;
		} catch (Exception e) {
			log.error("删除未审核企业详情失败", e);
			return false;
		}
	}
	
	/** 删除未审核公司产品*/
	public boolean deleteCompanyPorductUncheck(BigInteger id){
		try {
			companyMapper.deleteCompanyPorductUncheckById(id);
			return true;
		} catch (Exception e) {
			log.error("删除未审核企业详情失败", e);
			return false;
		}
	}
	
	/**
	 * 企业用户提交新公司到未审核公司表
	 * @param company 公司基本信息
	 * @param product 公司产品信息
	 * @param detail 公司详情信息
	 * @return
	 */
	public boolean userCommitCompany(Company company, CompanyProduct product,CompanyDetail detail){
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		try {
			detail.setGmt_create(timestamp);
			detail.setGmt_modified(timestamp);
			companyMapper.insertCompanyDetailUncheck(detail);
		} catch (Exception e2) {
			log.error("插入公司详情信息失败", e2);
			return false;
		}
		try {
			company.setCompany_detail(detail.getId());
			company.setGmt_create(timestamp);
			company.setGmt_modified(timestamp);
			companyMapper.insertCompanyUncheck(company);
		} catch (Exception e) {
			log.error("插入公司信息失败", e);
			try {
				//插入公司失败则把前面插入的公司详情删除
				companyMapper.deleteCompanyDetailUncheckById(detail.getId());
				return false;
			} catch (Exception e1) {
				log.error("删除公司详情失败", e1);
				return false;
			}
		}
		try {
			product.setProduct_company(company.getId());
			product.setGmt_create(timestamp);
			product.setGmt_modified(timestamp);
			companyMapper.insertProductUncheck(product);
		} catch (Exception e) {
			log.error("插入公司产品信息失败", e);
			try {
				//插入产品失败则把之前插入的公司和公司详情都删除
				companyMapper.deleteUncheckCompanyById(company.getId());
				companyMapper.deleteCompanyDetailUncheckById(detail.getId());
				return false;
			} catch (Exception e1) {
				log.error("删除公司和详情信息失败", e);
				return false;
			}
		}
		return true;
	}
	
	/** 根据id查询未审核公司*/
	public Company getCompanyUncheckById(BigInteger id){
		try {
			Company company = companyMapper.getCompanyUncheckById(id);
			company.setLocation_name(companyMapper.getLocationNameById(company.getCompany_location()));
			return company;
		} catch (Exception e) {
			log.error("根据id="+id+"查询未审核公司失败", e);
			return null;
		}
	}
	
	/** 根据id查询未审核公司详情*/
	public CompanyDetail getCompanyDetailUncheckById(BigInteger id){
		try {
			return companyMapper.getCompanyDetailUncheckById(id);
		} catch (Exception e) {
			log.error("根据id="+id+"查询未审核公司详情失败", e);
			return null;
		}
	}
	/** 根据公司id查询未审核产品*/
	public CompanyProduct getCompanyProductUncheckByCompanyId(BigInteger id){
		try {
			return companyMapper.getCompanyProductUncheckByCompanyId(id);
		} catch (Exception e) {
			log.error("根据公司id="+id+"查询未审核产品失败", e);
			return null;
		}
	}
	
	/**
	 * 添加新公司
	 * @param company 公司基本信息
	 * @param product 公司产品信息
	 * @param detail 公司详情信息
	 * @return
	 */
	public boolean addNewCompanyFromUncheck(Company company, CompanyProduct product,CompanyDetail detail){
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		try {
			detail.setGmt_create(timestamp);
			detail.setGmt_modified(timestamp);
			companyMapper.insertCompanyDetail(detail);
		} catch (Exception e2) {
			log.error("插入公司详情信息失败", e2);
			return false;
		}
		try {
			company.setCompany_detail(detail.getId());
			company.setGmt_create(timestamp);
			company.setGmt_modified(timestamp);
			companyMapper.insertCompany(company);
		} catch (Exception e) {
			log.error("插入公司信息失败", e);
			try {
				//插入公司失败则把前面插入的公司详情删除
				companyMapper.deleteCompanyDetailById(detail.getId());
				return false;
			} catch (Exception e1) {
				log.error("删除公司详情失败", e1);
				return false;
			}
		}
		try {
			product.setProduct_company(company.getId());
			product.setGmt_create(timestamp);
			product.setGmt_modified(timestamp);
			companyMapper.insertProduct(product);
		} catch (Exception e) {
			log.error("插入公司产品信息失败", e);
			try {
				//插入产品失败则把之前插入的公司和公司详情都删除
				companyMapper.deleteCompanyById(company.getId());
				companyMapper.deleteCompanyDetailById(detail.getId());
				return false;
			} catch (Exception e1) {
				log.error("删除公司和详情信息失败", e);
				return false;
			}
		}
		return true;
	}
	
	
}
